<script lang="ts" setup>
import {onMounted, ref} from 'vue';
import {message} from 'ant-design-vue';
import {Tool} from "@/util/tool";
import request from "@/service/request";
//
declare let hexMd5: any;
declare let KEY: any;
const pattern = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/;
const form = ref();
form.value = {};
const users = ref();
const user = ref();
const modalVisible = ref(false);
const modalLoading = ref(false);
const resetModalVisible = ref(false);
const resetModalLoading = ref(false);

const pagination = ref({
  current: 1,
  pageSize: 6,
  total: 0
});
const loading = ref(false);

const columns = [
  {
    title: '登陆名',
    dataIndex: 'loginName'
  },
  {
    title: '名称',
    dataIndex: 'name'
  },
  {
    title: '密码',
    dataIndex: 'password'
  },
  {
    title: 'Action',
    key: 'action',
    dataIndex: 'action'
  }
];

/**
 * 数据查询
 **/
const handleQuery = (params: any) => {
  loading.value = true
  request.get("/user/list", {
    params: {
      page: params.page,
      size: params.size,
      loginName: params.loginName
    }
  }).then((res) => {
    loading.value = false;
    users.value = res.data.content.list;
    // 重置分页按钮
    pagination.value.current = params.page;
    pagination.value.total = res.data.content.total;
  })
};

/**
 * 表格点击页码时触发
 */
const handleTableChange = (pagination: any) => {
  handleQuery({
    page: pagination.current,
    size: pagination.pageSize
  });
};

// -------- 表单 ---------
const handleModalOk = () => {
  if(!pattern.test(user.value.password)&&!user.value.id){
    message.warn("【密码】只包含 数字和英文，长度6-18!")
    return
  }
  modalLoading.value = true;
  user.value.password = hexMd5(user.value.password + KEY);
  request.post("/user/save", user.value).then((response) => {
    modalLoading.value = false;
    const data = response.data; // data = commonResp
    if (data.success) {
      modalVisible.value = false;
      // 重新加载列表
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize,
      });
    } else {
      message.error(data.message);
    }
  });
};

/**
 * 编辑
 */
const edit = (record: any) => {
  user.value = Tool.copy(record);
  modalVisible.value = true;
};

/**
 * 新增
 */
const add = () => {
  user.value = {};
  modalVisible.value = true;
};

const handleDelete = (id: number) => {
  request.delete("/user/delete/" + id).then((response) => {
    const data = response.data; // data = commonResp
    if (data.success) {
      // 重新加载列表
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize,
      });
    } else {
      message.error(data.message);
    }
  });
};

// -------- 重置密码 ---------
const handleResetModalOk = () => {
  if(!pattern.test(user.value.password)){
    message.warn("【密码】只包含 数字和英文，长度6-18!")
    return
  }
  resetModalLoading.value = true;
  user.value.password = hexMd5(user.value.password + KEY);

  request.post("/user/resetPassword", user.value).then((response) => {
    resetModalLoading.value = false;
    const data = response.data; // data = commonResp
    if (data.success) {
      resetModalVisible.value = false;
      // 重新加载列表
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize,
      });
    } else {
      message.error(data.message);
    }
  });
};

/**
 * 重置密码
 */
const resetPassword = (record: any) => {
  resetModalVisible.value = true;
  user.value = Tool.copy(record);
  user.value.password = null;
};

onMounted(() => {
  handleQuery({
    page: 1,
    size: pagination.value.pageSize,
  });
});

</script>

<template>
  <a-layout>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
      <p>
        <a-form layout="inline" :model="form">
          <a-form-item>
            <a-input-search
                v-model:value="form.loginName"
                placeholder="输入名字"
                enter-button
                @search="handleQuery({page: 1,size: pagination.pageSize,loginName: form.loginName})"
            />
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="add()">
              新增
            </a-button>
          </a-form-item>
        </a-form>
      </p>
      <a-table
          :columns="columns"
          :row-key="record => record.id"
          :data-source="users"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key==='action'">
            <a-space size="small">
              <a-button type="primary" @click="resetPassword(record)">
                重置密码
              </a-button>
              <a-button type="primary" @click="edit(record)">
                编辑
              </a-button>
              <a-popconfirm
                  title="删除后不可恢复，确认删除?"
                  ok-text="是"
                  cancel-text="否"
                  @confirm="handleDelete(record.id)"
              >
                <a-button type="danger">
                  删除
                </a-button>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-layout-content>
  </a-layout>

  <a-modal
      title="用户表单"
      v-model:visible="modalVisible"
      :confirm-loading="modalLoading"
      @ok="handleModalOk"
  >
    <a-form :model="user" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <a-form-item label="登陆名">
        <a-input v-model:value="user.loginName" :disabled="!!user.id"/>
      </a-form-item>
      <a-form-item label="昵称">
        <a-input v-model:value="user.name"/>
      </a-form-item>
      <a-form-item label="密码" v-show="!user.id">
        <a-input-password v-model:value="user.password" type="password"/>
      </a-form-item>
    </a-form>
  </a-modal>

  <a-modal
      title="重置密码"
      v-model:visible="resetModalVisible"
      :confirm-loading="resetModalLoading"
      @ok="handleResetModalOk"
  >
    <a-form :model="user" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <a-form-item label="新密码">
        <a-input v-model:value="user.password" type="password"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<style scoped>
img {
  width: 50px;
  height: 50px;
}
</style>
